package com.example.vv.model.self;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.vv.R;
import com.example.vv.fragmentWork.entity.LoginEntity;
import com.example.vv.fragmentWork.mvp.MvpFragment;
import com.example.vv.fragmentWork.utlis.ShareUtils;
import com.example.vv.fragmentWork.utlis.UserInfo;
import com.example.vv.model.login.LoginActivity;
import com.example.vv.model.self.base.contract.SelfContract;
import com.example.vv.model.self.base.presenter.SelfPresenter;
import com.example.vv.model.setting.SettingActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

public class SelfFragment extends MvpFragment<SelfContract.View, SelfPresenter> implements SelfContract.View {

    @BindView(R.id.self_icon_image)
    ImageView selfIconImage;
    @BindView(R.id.self_name_text)
    TextView selfNameText;
    @BindView(R.id.self_login)
    RelativeLayout selfLogin;

    UserInfo userInfo;
    @BindView(R.id.self_bg)
    ImageView selfBg;
    @BindView(R.id.self_l1)
    LinearLayout selfL1;
    @BindView(R.id.self_l2)
    LinearLayout selfL2;
    @BindView(R.id.self_l3)
    LinearLayout selfL3;
    @BindView(R.id.self_l4)
    LinearLayout selfL4;
    @BindView(R.id.self_l5)
    LinearLayout selfL5;
    @BindView(R.id.self_l6)
    LinearLayout selfL6;
    @BindView(R.id.self_l7)
    LinearLayout selfL7;
    @BindView(R.id.self_l8)
    LinearLayout selfL8;
    boolean isLogin;

    @Override
    public int initLayout() {
        return R.layout.fragment_self;
    }

    @Override
    public SelfPresenter initPresenter() {
        return new SelfPresenter();
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        if (ShareUtils.isLogin) {
            Glide.with(this)
                    .load(userInfo.getImgUrl())
                    .circleCrop()
                    .error(R.mipmap.abc)
                    .into(selfIconImage);
            selfNameText.setText(userInfo.getName());
        }
    }

    @Override
    public void lazyLoad() {

    }

    @Override
    public void initData() {
        isLogin = ShareUtils.isLogin;
        userInfo = ShareUtils.getUserInfo(getActivity());
    }

    @Override
    public void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleBar() {
        return null;
    }


    @Override
    public Context getMContext() {
        return getContext();
    }

    @OnClick(R.id.self_icon_image)
    public void onClick(View view) {
        if (isLogin) {
            Toast.makeText(mActivity, "您已经登录过了", Toast.LENGTH_SHORT).show();
        } else {
            startActivity(new Intent(getMContext(), LoginActivity.class));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void setUserName(LoginEntity entity) {
        if (entity != null) {
            selfNameText.setText(entity.getUsername());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.self_l1, R.id.self_l2, R.id.self_l3, R.id.self_l4, R.id.self_l5, R.id.self_l6, R.id.self_l7, R.id.self_l8})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.self_l1:
                break;
            case R.id.self_l2:
                break;
            case R.id.self_l3:
                break;
            case R.id.self_l4:
                break;
            case R.id.self_l5:
                break;
            case R.id.self_l6:
                break;
            case R.id.self_l7:
                break;
            case R.id.self_l8:
                intent.setClass(getActivity(), SettingActivity.class);
                startActivityForResult(intent,1);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==99) {
            String name = data.getStringExtra("name");
            if ("".equals(name)) {
                selfNameText.setText("未登录");
            }else {
                selfNameText.setText(name);
            }
        }
    }
}